package com.album.utils.down;

import java.util.ArrayList;
import java.util.List;

import android.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.album.bean.ImageBean;
import com.album.utils.MyLog;
import com.album.utils.down.AsyncImageLoaders.ImageCallback;

public class MyImageAdapter extends BaseAdapter {  
    private Context mContext;  
    
    private List<ImageBean> imageList = new ArrayList<ImageBean>();  
    private AsyncImageLoaders imageLoader;
    private Drawable drawable;
    
    public MyImageAdapter(Context c,List<ImageBean> imageList) {  
        mContext = c;  
        drawable = mContext.getResources().getDrawable(com.album.R.drawable.loading);
        this.imageList = imageList;
        
        imageLoader = new AsyncImageLoaders();
    }  

    public int getCount() {  
        return (imageList.size()>0) ? imageList.size():0;  
    }  

    public Object getItem(int position) {  
    	return (imageList.size()>0) ? imageList.get(position):null;   
    }  

    public long getItemId(int position) {  
        return position;  
    }  

    public View getView(int position, View convertView, ViewGroup parent) {    
        final ImageView imageView;           
        if (convertView == null) {      
            imageView=new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));  
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);   
            convertView = imageView;   
            if(imageList!=null && imageList.size()>0){
            	  String url = imageList.get(position).getImageUrl();
                  drawable = imageLoader.loadDrawable(url, new ImageCallback() {  

                      public void imageLoaded(Drawable imageDrawable, String imageUrl) {  
                         // MyLog.debug(getClass(), "===========imageDrawable =======:"+imageDrawable.getIntrinsicHeight());
                          if(imageDrawable!=null)
                          	imageView.setImageDrawable(imageDrawable);  
                      }  
                  });   
            }
          
            imageView.setImageDrawable(drawable);
        }  
          
        return convertView;  
    }  
}  
